//
//  QLoginVC.m
//  QoilPublic
//
//  Created by 杨凯 on 16/7/4.
//  Copyright © 2016年 杨凯. All rights reserved.
//

#import "QLoginVC.h"
#import "QRegisterVC.h"
#import "QForgetPassWordVC.h"
#import "QWebViewVC.h"
#import "JPUSHService.h"
//#import "CodeLoginVC.h"
@interface QLoginVC ()<UITextFieldDelegate>

@property (weak, nonatomic) IBOutlet UIView *loginBgView;

@property (weak, nonatomic) IBOutlet UIImageView *horLine;

@property (weak, nonatomic) IBOutlet NSLayoutConstraint *horLineConstr;

@property (weak, nonatomic) IBOutlet UITextField *phoneTextField;//手机号

@property (weak, nonatomic) IBOutlet UITextField *passWordField;//密码

@property (weak, nonatomic) IBOutlet UIButton *loginButton;

@property (weak, nonatomic) IBOutlet UIButton *selectButton;

@property (weak, nonatomic) IBOutlet UIButton *regButton;

@property (weak, nonatomic) IBOutlet UIImageView *twoLine;

@property (weak, nonatomic) IBOutlet NSLayoutConstraint *twoLineConstr;

@property (weak, nonatomic) IBOutlet NSLayoutConstraint *topConstrain;

@end

@implementation QLoginVC{

    
    NSString *machineCode;
    BOOL oneBool,twoBool;
}

- (id)init{
    self = [super init];
    if (self) {
        
        self.hidesBottomBarWhenPushed = YES;
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"登录";
    self.view.backgroundColor = [UIColor whiteColor];
    [self setupAttbuild];
}

- (void)setupAttbuild{

    [self setupForDismissKeyboard];//释放键盘
    [_loginButton blueStyle];
   _loginBgView.backgroundColor = [UIColor whiteColor];
    _horLine.backgroundColor = [QColorUtility defaultLineColor];
    _horLineConstr.constant = 0.5;
    
    _twoLine.backgroundColor = [QColorUtility defaultLineColor];
    _twoLineConstr.constant = 0.5;
    
    _regButton.layer.borderWidth = 0.5;
    _regButton.layer.borderColor = [QColorUtility defaultLineColor].CGColor;
    
    [QViewUtility setupNavigationBarBackForController:self backSelector:@selector(Loginback)];
    
    _phoneTextField.tag = 1;
    _phoneTextField.delegate = self;
    [_phoneTextField addTarget:self action:@selector(textfieldAction:) forControlEvents:UIControlEventEditingChanged];
    
    _passWordField.tag = 2;
    _passWordField.delegate = self;
    _passWordField.secureTextEntry = YES;
    [_passWordField addTarget:self action:@selector(textfieldAction:) forControlEvents:UIControlEventEditingChanged];
    
    if(iphone4x_3_5){
        self.topConstrain.constant = 80;
    }else if(iphone5x_4_0){
        self.topConstrain.constant = 90;
    }else if (iphone6_4_7){
        self.topConstrain.constant = 120;
    }else if (iphone6Plus_5_5){
        self.topConstrain.constant = 150;
    }
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField{
    [textField resignFirstResponder];
    return YES;
}

- (void)textfieldAction:(UITextField *)textField{
    
    if(textField.tag == 1){
        CLog(@"电话==%@",textField.text);
        if(textField.text.length == 11){
            oneBool = YES;
        }else{
            oneBool = NO;
        }
        
    }else if (textField.tag == 2){
        if(textField.text.length > 0){
            twoBool = YES;
        }else{
            twoBool = NO;
        }
        CLog(@"密码===%@",textField.text);
    }
}

#pragma mark---返回
- (void)Loginback{
    if (_LoginAnimationStyle == LoginAnimationStyle_mySelf) {
        [self.navigationController popViewControllerAnimated:NO];
        [[NSNotificationCenter defaultCenter]postNotificationName:@"PopToMainVCNotification" object:nil];
    }else{
         [self.navigationController popViewControllerAnimated:YES];
    }
}

#pragma mark -- 验证码登陆
- (IBAction)VerificationCodeLandingAction:(UIButton *)sender {
    
//    CodeLoginVC *login = [[CodeLoginVC alloc]init];
//    [self.navigationController pushViewController:login animated:YES];
}

#pragma mark---注册
- (IBAction)registerAction:(id)sender {
    
    QRegisterVC *vc = [[QRegisterVC alloc] initWithNibName:@"QRegisterVC" bundle:nil];
    [self.navigationController pushViewController:vc animated:NO];
    CLog(@"注册");
}

#pragma mark---登录
- (IBAction)loginAction:(id)sender {
    if(oneBool && twoBool){
        [self requestData];
    }else{
        if(!oneBool){
            if(_phoneTextField.text.length == 0){
                [QViewUtility showAlertViewWithImage:[UIImage imageNamed:@"smile_image"] content:@"请填写手机号码" btnTitle:showAlertBtnTitle type:1 myBlock:^{}];
            }else{
                 [QViewUtility showAlertViewWithImage:[UIImage imageNamed:@"smile_image"] content:@"请填写正确手机号码" btnTitle:showAlertBtnTitle type:1 myBlock:^{}];
            }
        }else if (!twoBool){
            [QViewUtility showAlertViewWithImage:[UIImage imageNamed:@"smile_image"] content:@"请填写密码" btnTitle:showAlertBtnTitle type:1 myBlock:^{}];
        }
    }
}

#pragma mark---忘记密码
- (IBAction)forgetPassWorkBtn:(id)sender {
    
    QForgetPassWordVC *vc = [[QForgetPassWordVC alloc] init];
    vc.title = @"找回密码";
    [self.navigationController pushViewController:vc animated:YES];
}

- (IBAction)selectAction:(id)sender {
    
    UIButton *button = (UIButton *)sender;
    if(button.selected){
        
    }else{
    }
    button.selected = !button.selected;
}

- (IBAction)agreeAction:(id)sender {
    
    QWebViewVC *vc = [[QWebViewVC alloc] init];
    vc.style = WebLocalityStyle;
    vc.title = @"用户使用协议";
    [self.navigationController pushViewController:vc animated:YES];
}

#pragma mark---调用登录接口
- (void)requestData{
    NSTimeInterval inter = [[NSDate date] timeIntervalSince1970] * 1000;//当前时间戳
    NSString *timec = [NSString stringWithFormat:@"ios_%.f",inter];
    machineCode = [NSString stringWithFormat:@"%@%@",timec,_phoneTextField.text];
    [JPUSHService setAlias:machineCode callbackSelector:@selector(tagsAliasCallback:tags:alias:) object:self];
     [self requestLoginData];
}

- (void)tagsAliasCallback:(int)iResCode tags:(NSSet*)tags alias:(NSString*)alias {
    NSLog(@"rescode: %d, \ntags: %@, \nalias: %@\n", iResCode, tags , alias);
    if(iResCode == 0){
        CLog(@"绑定别名成功");
    }else{
        CLog(@"绑定别名失败");
    }
}

- (void)requestLoginData{

    NSString *url = [QParamService urlWithAction:@"loginUser"];
    NSString *md5Str = [QMD5Hash md5String:self.passWordField.text];
    md5Str = [md5Str uppercaseString];
    
    NSDictionary *param = [QParamService loginWithtel:self.phoneTextField.text password:md5Str machineCode:machineCode];
    [QViewUtility showMBHudToView:self.view hideBg:NO];
    
    [[QHttpManager sharedManager] POST:url parameterMD5:param completion:^(NSData *data, NSDictionary *dict, NSError *error) {
        [QViewUtility hideMBHudToView:self.view];
        if(error){
            return ;
        }
        QBaseModel *basModel = [QBaseModel objectFromDict:dict];
        if([basModel.result isEqualToString:@"success"]){
//            [QViewUtility showAlertViewWithImage:[UIImage imageNamed:@"smile_image"] content:@"登录成功" btnTitle:showAlertBtnTitle type:1 myBlock:^{}];
            QLoginModel *loginModel = [QLoginModel objectFromDict:basModel.items];
            [QDataUtility updateLoginModel:loginModel];//更新model
            [[NSNotificationCenter defaultCenter] postNotificationName:kNotificationLoginSignup object:nil];
            [self.navigationController popViewControllerAnimated:YES];
            
        }else{
            
            [QViewUtility showAlertViewWithImage:[UIImage imageNamed:@"cry_image"] content:basModel.errMsg btnTitle:showAlertBtnTitle type:1 myBlock:^{}];            
        }
    }];
}


@end
